import React, { useEffect, useState } from "react";
import { Space, Table, Image, Card, Modal, Button, Drawer } from "antd";
import type { ColumnsType } from "antd/es/table";
import useModal from "@/hooks/useModal";
import useMessage from "@/hooks/useMessage";
import PhotoAdd from "../PhotoAdd";
import styles from "./index.module.less";
import { Photo } from "@/api/interface/album";
import { deletePhotoApi, getPhotoListApi } from "@/api/modules/album";
import usePagination from "@/hooks/usePagination";
import PhotoFilter from "../PhotoFilter";

export interface DataType {
	id: number;
	key: number;
	album_id: number;
	photo_url: string;
	createdAt: string;
}
[];

const PhotoList: React.FC = props => {
	const columns: ColumnsType<DataType> = [
		{
			title: "id",
			dataIndex: "id",
			key: "id"
		},
		{
			title: "图片",
			dataIndex: "photo_url",
			key: "photo_url",
			render: (_, row) => {
				return <Image width={200} height={100} src={row.photo_url} />;
			}
		},
		{
			title: "创建日期",
			dataIndex: "createdAt",
			key: "createdAt"
		},
		{
			title: "操作",
			key: "action",
			render: (_, row) => {
				return (
					<>
						<Space size="middle">
							<a onClick={() => openEdit(row)}>修改</a>
							<a onClick={() => openModal(row.id)}>删除</a>
						</Space>
					</>
				);
			}
		}
	];

	const listExtra = (
		<div style={{ display: "flex", gap: 8 + "px" }}>
			<Button type="primary" onClick={() => setAddVisible(true)}>
				新增
			</Button>
			<Button type="primary" onClick={() => getPhotoList()}>
				刷新
			</Button>
		</div>
	);

	const { contextHolder, success, error, warning } = useMessage();
	const { id, isModalOpen, openModal, closeModal } = useModal();

	const [photoList, setPhotoList] = useState<Photo[]>([]);
	const [photoTotal, setPhotoTotal] = useState(0);
	const [photo, setPhoto] = useState<DataType>();
	const [currentAlbum, setCurrentAlbum] = useState<number>();

	const getPhotoList = async (page: number = 1, size: number = 10) => {
		if (!currentAlbum) return warning("请先选择相册");
		const res = await getPhotoListApi(currentAlbum, page, size);
		if (res.code === 200) {
			setPhotoList(res.data.rows);
			setPhotoTotal(res.data.total);
		}
	};

	useEffect(() => {
		getPhotoList(1, 10);
	}, [currentAlbum]);

	const { onChangePage } = usePagination(getPhotoList);

	const deletePhoto = async () => {
		const res = await deletePhotoApi([id]);
		if (res.code === 200) {
			getPhotoList(1, 10);
			success("删除成功");
		} else {
			error("删除失败");
		}
		closeModal();
	};

	const data: any = photoList.map(item => {
		return {
			id: item.id,
			key: item.id,
			album_id: item.album_id,
			photo_url: item.photo_url,
			createdAt: item.createdAt
		};
	});

	// 修改
	const [editVisible, setEditVisible] = useState(false);
	const openEdit = async (photo: DataType) => {
		setPhoto(photo);
		setEditVisible(true);
	};
	const onCloseEdit = () => {
		getPhotoList();
		setEditVisible(false);
	};

	// 新增
	const [addVisible, setAddVisible] = useState(false);
	const onCloseAdd = () => {
		getPhotoList();
		setAddVisible(false);
	};

	// 搜索
	const onSearch = (albumId?: number) => {
		setCurrentAlbum(albumId);
	};

	return (
		<div className={styles.root}>
			{contextHolder}
			<PhotoFilter onSearch={onSearch} />
			<Card title="数据列表" extra={listExtra}>
				<Table columns={columns} dataSource={data} pagination={{ total: photoTotal, onChange: onChangePage }} />
			</Card>

			<Modal title="提醒" mask={false} visible={isModalOpen} onOk={deletePhoto} onCancel={closeModal}>
				确定删除该图片吗
			</Modal>

			<Drawer
				title="新增"
				placement="right"
				width={500}
				onClose={() => {
					setAddVisible(false);
				}}
				visible={addVisible}
			>
				<PhotoAdd currentAlbum={currentAlbum} onComfirm={onCloseAdd}></PhotoAdd>
			</Drawer>

			<Drawer
				title="修改"
				placement="right"
				width={500}
				onClose={() => {
					setEditVisible(false);
				}}
				visible={editVisible}
			>
				<PhotoAdd type="edit" currentAlbum={currentAlbum} photo={photo} onComfirm={onCloseEdit}></PhotoAdd>
			</Drawer>
		</div>
	);
};

export default PhotoList;
